package com.appointment.controller.admin;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.appointment.dao.ScoresDao;
import com.appointment.entity.Confirm;
import com.appointment.entity.Score;
import com.appointment.entity.confirmPage;
import com.appointment.entity.scoresPage;
import com.appointment.web.PrintTimeFunction;

@Controller
@RequestMapping("/admin")
public class AdminScoreController {
	
	@Resource
	ScoresDao scoresdao;
	
	//未处理信息
	@Transactional(rollbackFor={Exception.class})
	@RequestMapping("/getscoresMes")
	public String getConfirmMes(@RequestParam(value="thispage", required=false) String thispage, Model model){
		if(thispage!=null&&!thispage.matches("[0-9]+")){
			return "redirect:/error";
		}
		scoresPage page = new scoresPage();
		if(thispage!=null){
			page.setThispage(Integer.parseInt(thispage));
		}
		
		List<Score> list = scoresdao.findscores(page);
		int num = scoresdao.findnums();
		page.setCountpage(num/page.getCountrow()+1);
		page.setList(list);
		for(Score c :list){
			c.setDay(PrintTimeFunction.getNiceDate(c.getTime()));
		}
		model.addAttribute("page", page);
		return "admin/adminSco";
		
	}
	
	//处理信息
	@Transactional(rollbackFor={Exception.class})
	@RequestMapping("/getscoresedMes")
	public String getConfirmedMes(@RequestParam(value="thispage", required=false) String thispage, Model model){
		if(thispage!=null&&!thispage.matches("[0-9]+")){
			return "redirect:/error";
		}
		scoresPage page = new scoresPage();
		if(thispage!=null){
			page.setThispage(Integer.parseInt(thispage));
		}
		
		List<Score> list = scoresdao.findscoresed(page);
		int num = scoresdao.findnumed();
		page.setCountpage(num/page.getCountrow()+1);
		page.setList(list);
		for(Score c :list){
			c.setDay(PrintTimeFunction.getNiceDate(c.getTime()));
		}
		model.addAttribute("page", page);
		return "admin/adminScowc";
		
	}
	
	@RequestMapping("/getscoresStu")
	public String getconfirmStu(String stu_number, Model model){
		Score score = scoresdao.findScoredByStuNumber(stu_number);
		if(score==null){
			return "redirect:/error";
		}
		model.addAttribute("score", score);
		return "admin/scorequi";
	}
	
	@RequestMapping("/updatescoresStu")
	public String updatescoreStu(String stu_number){
		Score score = scoresdao.findScoredByStuNumber(stu_number);
		if(score==null){
			return "redirect:/error";
		}
		scoresdao.updateScoresById(score.getId());
		return "redirect:/admin/getscoresMes";
	}
	
	
	@RequestMapping("/errorscoresStu")
	public String errorscoreStu(String stu_number){
		Score score = scoresdao.findScoredByStuNumber(stu_number);
		if(score==null){
			return "redirect:/error";
		}
		scoresdao.errorScoresById(score.getId());
		return "redirect:/admin/getscoresMes";
	}
}
